/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InicioJF.java
 *
 * Created on 30/09/2011, 08:49:50 AM
 */
package GUI;

import Control.FuncionesJpaController;
import Control.PermisosJpaController;
import Control.UsuarioJpaController;
import Control.exceptions.PreexistingEntityException;
import Entidad.Funciones;
import Entidad.Permisos;
import Entidad.Usuario;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author pepe
 */
public class InicioJF extends javax.swing.JFrame {

    
    Usuario u = new Usuario();
    Funciones f1 ;
    Funciones f2 ;
    Funciones f3 ;
    Permisos p = new Permisos();
    List<Funciones> listaFunciones = new ArrayList<Funciones>();
    List<Usuario> listaUsuario = new ArrayList<Usuario>();
    UsuarioJpaController controlUsuario = new UsuarioJpaController();
    FuncionesJpaController controlFunciones = new FuncionesJpaController();
    PermisosJpaController controlPermisos = new PermisosJpaController();
    JFrame segundo;
    
    
    /** Creates new form InicioJF */
    public InicioJF() throws PreexistingEntityException, Exception {
        this.setTitle("LogIn");
        
        initComponents();
        contraseniaTF.addKeyListener(new KeyAdapter(){
            @Override
            public void keyTyped(KeyEvent e) {
                char caracter = e.getKeyChar();
                if(caracter == KeyEvent.VK_ENTER){
                    ingresarJBActionPerformed(null);
                }
            }
        });
        
//        u = new Usuario();
//        
//        u.setNombre("Pedro Ceron");
//        u.setCedula(1022365758l);
//        u.setTelefono(12345);
//        u.setCorreoElectronico("pmcerons@unal.edu.co");
//        u.setContraseña("1022365758");
//        u.setFechaDeNacimiento(new Date(90, 11, 21));
//        u.setUser("pmcerons");
//        
//        controlUsuario.create(u);
//        
//        
//        f1 = new Funciones(1001);
//        f1.setNombre("Administrador");
//        controlFunciones.create(f1);
//        
//        f2 = new Funciones(1002);
//        f2.setNombre("Monitoreador Radio");        
//        controlFunciones.create(f2);
//        
//        f3 = new Funciones(1003);
//        f3.setNombre("Monitoreador Television");        
//        controlFunciones.create(f3);
//        
//        p.setUsuario(u);
//        p.setFunciones(f1);        
//        controlPermisos.create(p);
//            
//        p.setUsuario(u);
//        p.setFunciones(f2);        
//        controlPermisos.create(p);
//           
//        p.setUsuario(u);
//        p.setFunciones(f3);        
//        controlPermisos.create(p);
//        
//        u = new Usuario(1l);
//        u.setNombre("Monitoreador de television");
//        u.setUser("monTV");
//        u.setContraseña("1234");
//        controlUsuario.create(u);
//        
//        p = new Permisos();
//        p.setUsuario(u);
//        p.setFunciones(f3);
//        controlPermisos.create(p);
//        
//        u = new Usuario(2l);
//        u.setNombre("Monitoreador de radio");
//        u.setUser("monRAD");
//        u.setContraseña("4321");
//        controlUsuario.create(u);
//        
//        p = new Permisos();
//        p.setUsuario(u);
//        p.setFunciones(f2);
//        controlPermisos.create(p);
             
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        userTF = new javax.swing.JTextField();
        ingresarJB = new javax.swing.JButton();
        informationJL = new javax.swing.JLabel();
        contraseniaTF = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        userTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userTFActionPerformed(evt);
            }
        });

        ingresarJB.setText("Ingresar");
        ingresarJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ingresarJBActionPerformed(evt);
            }
        });

        contraseniaTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                contraseniaTFActionPerformed(evt);
            }
        });

        jLabel1.setText("Usuario");

        jLabel2.setText("Contraseña");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(ingresarJB)
                        .addGap(70, 70, 70))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(informationJL, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2))
                            .addGap(13, 13, 13)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(userTF)
                                .addComponent(contraseniaTF, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(contraseniaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(informationJL, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(ingresarJB)
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void userTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userTFActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_userTFActionPerformed

private void ingresarJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ingresarJBActionPerformed
    u = new Usuario();
    
    u = controlUsuario.encontrarUsuarioPorUser(userTF.getText(), contraseniaTF.getText());
    
    if(u != null){
        segundo= new MenuP(u);
        
        segundo.show();
        this.hide();
    }else{
        informationJL.setText("usuario o contraseñaincorrectos");
    }
    
    userTF.setText("");
    contraseniaTF.setText("");
    informationJL.setText("");
    
    
}//GEN-LAST:event_ingresarJBActionPerformed

private void contraseniaTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_contraseniaTFActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_contraseniaTFActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(InicioJF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(InicioJF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(InicioJF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(InicioJF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//// JFrame.setDefaultLookAndFeelDecorated(true);
////      SwingUtilities.invokeLater(new Runnable() {
////      public void run() {
////        try {
////          UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceGraphiteLookAndFeel");
////         // UIManager.("oktoberfest.jpge");
////         //SubstanceImageWatermark("c:\\asd.JPEG");
////        //SubstanceLookAndFeel.WATERMARK_VISIBLE;
////         //org.pushingpixels.substance.api.skin.SubstanceTwilightLookAndFeel
////        } catch (Exception e) {
////          System.out.println("Substance Graphite failed to initialize");
////        }
////        MenuP w = new MenuP();
////        w.setVisible(true);
////      }
//            public void run() {
//                try {
//                    new InicioJF().setVisible(true);
//                } catch (PreexistingEntityException ex) {
//                    Logger.getLogger(InicioJF.class.getName()).log(Level.SEVERE, null, ex);
//                } catch (Exception ex) {
//                    Logger.getLogger(InicioJF.class.getName()).log(Level.SEVERE, null, ex);
//                }
//            }
//        });
//    }
    
    public static void main(String args[]) {

        JFrame.setDefaultLookAndFeelDecorated(true);

        /* Create and display the form */
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceGraphiteLookAndFeel");
                    new InicioJF().setVisible(true);
                } catch (PreexistingEntityException ex) {
                    Logger.getLogger(InicioJF.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    System.out.println("Substance Graphite failed to initialize");
                    Logger.getLogger(InicioJF.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPasswordField contraseniaTF;
    private javax.swing.JLabel informationJL;
    private javax.swing.JButton ingresarJB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField userTF;
    // End of variables declaration//GEN-END:variables
}
